﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.Forms;
namespace BusinessLibrary
{
    /// <summary>
    /// 选项界面
    /// </summary>
    public partial class FormOptions : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormOptions()
        {
            InitializeComponent();
        }
        private void tabControlSettings_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormOptions_Load(object sender, EventArgs e)
        {
            LoadAppBasicSettings();
            this.labelStart.Select();
        }
        /// <summary>
        /// 加载设置内容
        /// </summary>
        private void LoadAppBasicSettings()
        {
            AppBasicSetting appBasicSetting = AppBasicSetting.Instance();
            textBoxEquipmentCode.Text = appBasicSetting.EquipmentCode;
            textBoxSiteCode.Text = appBasicSetting.SiteCode;
            textBoxLineCode.Text = appBasicSetting.LineCode;
            textBoxSoftwareName.Text = appBasicSetting.SoftwareName;
            radioButton3153ActivePC.Checked = appBasicSetting.HIOKI3153ActiveUnit.Equals(1);
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (GlobalStatus.APP == EAppStatus.RUN)
            {
                HWMessageBox.Show(this, "设备运行中，不允许修改参数");
                return;
            }
            bool checkPermission = UserHelper.CheckPermission(this, 3);
            if (checkPermission)
            {
                bool isValid = true;
                var eCode = textBoxEquipmentCode.Text.Trim();
                var sCode = textBoxSiteCode.Text.Trim();
                var lCode = textBoxLineCode.Text.Trim();
                var sName = textBoxSoftwareName.Text.Trim();
                var hioki3153 = radioButton3153ActivePC.Checked;

                if (!string.IsNullOrWhiteSpace(sCode) && !string.IsNullOrWhiteSpace(eCode) && !string.IsNullOrWhiteSpace(sName) && isValid)
                {
                    AppBasicSetting appBasicSetting = AppBasicSetting.Instance();
                    appBasicSetting.EquipmentCode = eCode;
                    appBasicSetting.SiteCode = sCode;
                    appBasicSetting.LineCode = lCode;
                    appBasicSetting.SoftwareName = sName;
                    appBasicSetting.HIOKI3153ActiveUnit = hioki3153 ? 1 : 2;
                    appBasicSetting.Save();
                }
                else
                {
                    HWMessageBox.Show(this, "设备编码，站点编码，软件名称必须有值");
                    isValid = false;
                }
                if (isValid)
                {
                    this.DialogResult = DialogResult.OK;
                    HWMessageBox.Show(this, "保存成功");
                }
            }
        }
        private void FormOptions_Shown(object sender, EventArgs e)
        {
        }
        private void FormOptions_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
    }
}
